/*
 * Copyright (C) 2004, Velocityme Partnership
 * GroupActions.java
 *
 * Created on 09 October 2004, 12:23
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.*;
import com.velocityme.www.actionforms.nodeactionforms.GroupValueActionForm;
import com.velocityme.www.dto.IntegerString;
import com.velocityme.www.utility.NodeUtil;
import java.util.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;

/**
 *
 * @author  Robert
 */
public class GroupActions extends NodeActions {
    
    /** Creates a new instance of GroupActions */
    public GroupActions() {
    }
    
    public ActionForward show(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        GroupPK groupPK = new GroupPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("groupValue", cs.getGroupValue(key, groupPK));
        session.setAttribute("userValues", cs.getGroupUserValues(key, groupPK));
        return mapping.findForward("ACTION_DISPLAY");
    }
    
    public ActionForward showCreate(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        GroupPK groupPK = new GroupPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("parentNodePK", nodePK);
        session.setAttribute("unassignedUserPairs", NodeUtil.getAllUserPairs(key, cs));
        session.setAttribute("assignedUserPairs", new ArrayList());
        GroupValueActionForm groupValueActionForm = new GroupValueActionForm();
        session.setAttribute("groupActionForm", groupValueActionForm);
        GroupValue groupValue = new GroupValue();
        groupValue.setNodeValue(new NodeValue());
        session.setAttribute("groupValue", groupValue);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward showEdit(ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        GroupPK groupPK = new GroupPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        GroupValue groupValue = cs.getGroupValue(key, groupPK);
        session.setAttribute("groupValue", groupValue);
        GroupValueActionForm groupValueActionForm = new GroupValueActionForm();
        groupValueActionForm.setName(groupValue.getNodeValue().getName());
        groupValueActionForm.setDescription(groupValue.getNodeValue().getDescription());
        session.setAttribute("groupActionForm", groupValueActionForm);
        Collection assignedUserPairs = new ArrayList();
        Iterator i = cs.getGroupUserValues(key, groupPK).iterator();
        while(i.hasNext()) {
            UserValue userValue = (UserValue)i.next();
            assignedUserPairs.add(new IntegerString(userValue.getUserId(), userValue.getUserName()));
        }
        Collection allUserPairs = NodeUtil.getAllUserPairs(key, cs);
        allUserPairs.removeAll(assignedUserPairs);
        session.setAttribute("unassignedUserPairs", allUserPairs);
        session.setAttribute("assignedUserPairs", assignedUserPairs);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward edit(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodePK nodePK;
        if (request.getParameter("btnSubmit").equals("OK")) {
            GroupValue groupValue = (GroupValue)session.getAttribute("groupValue");
            GroupValueActionForm groupForm = (GroupValueActionForm)form;
            groupValue.getNodeValue().setName(groupForm.getName());
            groupValue.getNodeValue().setDescription(groupForm.getDescription());
            GroupPK groupPK;
            if (groupValue.groupIdHasBeenSet()) {
                groupPK = groupValue.getPrimaryKey();
                cs.groupEdit(key, groupPK, groupValue, groupForm.getAssignedUserPKs());
            }
            else {
                NodePK parentNodePK = (NodePK)session.getAttribute("parentNodePK");
                groupPK = cs.groupCreate(key, parentNodePK, groupValue, groupForm.getAssignedUserPKs());
            }
            nodePK = new NodePK(groupPK.getGroupId());
        }
        // Cancel simply displays the original node again
        else {
            nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        }
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
}
